package com.buding.fx.validators;

import com.jfoenix.controls.JFXTextField;
import com.jfoenix.validation.base.ValidatorBase;
import javafx.scene.control.TextInputControl;

public class RequireAndLengthValidator extends ValidatorBase {
    private int lehgth;
    public RequireAndLengthValidator(int length){
        this.lehgth = length;
    }

    @Override
    protected void eval() {
        JFXTextField textField = (JFXTextField) srcControl.get();
        String text = textField.getText();
        if (text.isEmpty()){
            setMessage("所输入的字段不能为空");
            hasErrors.set(true);
        }else{
            if (text.length() > lehgth - 1){
                setMessage("所输入的字段长度不能超过"+lehgth);
                hasErrors.set(true);
            }else{
                hasErrors.set(false);
            }
        }
    }
}
